package com.eorchis.module.predeptassignedinfo.ui.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.core.service.IBaseService;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.commond.converter.IQueryCommondAttributeConverter;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.deptassignedinfo.domain.DeptAssignedInfo;
import com.eorchis.module.deptassignedinfo.service.IDeptAssignedInfoService;
import com.eorchis.module.deptassignedinfo.ui.commond.DeptAssignedInfoValidCommond;
import com.eorchis.module.predeptassignedinfo.domain.PreDeptAssignedInfo;
import com.eorchis.module.predeptassignedinfo.domain.PreDeptAssignedInfoBean;
import com.eorchis.module.predeptassignedinfo.service.IPreDeptAssignedInfoService;
import com.eorchis.module.predeptassignedinfo.ui.commond.PreDeptAssignedInfoQueryCommond;
import com.eorchis.module.predeptassignedinfo.ui.commond.PreDeptAssignedInfoValidCommond;
import com.eorchis.module.signupdept.domain.SignupDept;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: PreDeptAssignedInfoController<br>
 * Description: 单位年度分配参学预授权名额表管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Administrator
 * @createDate 2015-01-25
 * @version $Revision$
 */
@Controller("preDeptAssignedInfoController")
@RequestMapping(value = PreDeptAssignedInfoController.MODULE_PATH)
public class PreDeptAssignedInfoController extends AbstractBaseController<PreDeptAssignedInfoValidCommond,PreDeptAssignedInfoQueryCommond> {
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/predeptassignedinfo";
	// 单位年度预分配参学名额表接口
	@Resource(name = "com.eorchis.module.predeptassignedinfo.service.impl.PreDeptAssignedInfoServiceImpl")
	private IPreDeptAssignedInfoService preDeptAssignedInfoService;
	// 单位年度参学名额表接口
	@Resource(name = "com.eorchis.module.deptassignedinfo.service.impl.DeptAssignedInfoServiceImpl")
	private IDeptAssignedInfoService deptAssignedInfoService;
	@Autowired
	@Qualifier("attributeConverter")
	private ArrayList<IQueryCommondAttributeConverter> attributeConverter;
	@Override
	public IBaseService getService() {
		return preDeptAssignedInfoService;
	}

	@Override
	public String getModulePath() {
		return MODULE_PATH;
	}
	/**
	 * 预分配参学名额的列表
	 * @param querycommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/findPreDeptAssignedInfoList")
	public String findPreDeptAssignedInfoList(
			@ModelAttribute("resultList") PreDeptAssignedInfoQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//获得传过来的参数，默认是普通班
		if(!PropertyUtil.objectNotEmpty(queryCommond.getSearchClassType())){
			queryCommond.setSearchClassType(DeptAssignedInfo.ORDINARY_CLASS);
		}
		//默认查询当前年
		if(!PropertyUtil.objectNotEmpty(queryCommond.getSearchYear())){
			Calendar a=Calendar.getInstance();  
			queryCommond.setSearchYear(a.get(Calendar.YEAR));
		}
		if (attributeConverter != null) {
			parameterValueConver(queryCommond, request, attributeConverter);
		}
		//调用service层查询预分配参学名额列表的方法
		List<PreDeptAssignedInfoBean> result = preDeptAssignedInfoService.findPreDeptAssignedInfoList(queryCommond);
		queryCommond.setResultList(result);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("查询成功！");
		return "";
	}
	/**
	 * 用于排序的方法
	 * @param queryCommond
	 * @param request
	 * @param attributeConverter
	 */
	@SuppressWarnings("unchecked")
	private void parameterValueConver(PreDeptAssignedInfoQueryCommond queryCommond,
			HttpServletRequest request,
			List<IQueryCommondAttributeConverter> attributeConverter) {
		for (IQueryCommondAttributeConverter queryCommondAttributeConverter : attributeConverter) {
			queryCommondAttributeConverter.convert(queryCommond,
					request.getParameterMap());
		}
	}
	/**
	 * 修改预分配参学名额列表上的限定人数
	 * @param validCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/updatePreDeptAssignedInfo")
	public String updatePreDeptAssignedInfo(@ModelAttribute("result") PreDeptAssignedInfoValidCommond validCommond,
			HttpServletRequest request,HttpServletResponse response, 
			@ModelAttribute("resultState") ResultState resultState)throws Exception {
		//获得前端传过来的参数
		if (PropertyUtil.objectNotEmpty(validCommond.getId())&&PropertyUtil.objectNotEmpty(validCommond.getRestrictPersonNum())) {
			//调用service层更新限定人数的方法
			boolean result = preDeptAssignedInfoService.updatePreDeptAssignedInfo(validCommond);
			if (!result) {
				resultState.setState(ResultState.RESULT_STATE_FAIL);
				resultState.setMessage("更新失败！");
			}else {
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
				resultState.setMessage("更新成功！");
			}
		}else{
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			resultState.setMessage("未传入要更新的记录!");
		}
		return "";
	}
	/**
	 * 预分配参学名额:未选单位列表
	 * @param querycommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/findNoSelectSignupDeptList")
	public String findNoSelectSignupDeptList(
			@ModelAttribute("resultList") PreDeptAssignedInfoQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//获得传过来的参数
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchParentId())){
			if (attributeConverter != null) {
				parameterValueConver(queryCommond, request, attributeConverter);
			}
			//获得传过来的参数，默认是普通班
			if(!PropertyUtil.objectNotEmpty(queryCommond.getSearchClassType())){
				queryCommond.setSearchClassType(PreDeptAssignedInfo.ORDINARY_CLASS);
			}
			//查询当前年
			Calendar a=Calendar.getInstance();  
			int year=a.get(Calendar.YEAR);
			queryCommond.setSearchYear(year);
			//调用service层查询预分配参学名额:未选单位列表的方法
			List<PreDeptAssignedInfoBean> result = preDeptAssignedInfoService.findNoSelectSignupDeptList(queryCommond);
			queryCommond.setResultList(result);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("查询成功！");
		}else{
			resultState.setMessage("未选中任何单位！");
		}
		return "";
	}
	/**
	 * 添加单位年度预分配参学名额
	 * @param validCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/addPreDeptAssignedInfo")
	public String addPreDeptAssignedInfo(@ModelAttribute("result") PreDeptAssignedInfoValidCommond validCommond,
			HttpServletRequest request,HttpServletResponse response, 
			@ModelAttribute("resultState") ResultState resultState)throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//获得前端传过来的参数
		if (PropertyUtil.objectNotEmpty(validCommond.getIds())) {
			Calendar a=Calendar.getInstance();  
			int year=a.get(Calendar.YEAR);
			int restrictPersonNum=0;
			for(int i=0;i<validCommond.getIds().length;i++){
				PreDeptAssignedInfoValidCommond vCommond = new PreDeptAssignedInfoValidCommond();
				vCommond.setYear(year);//年份
				//如果是普通班:报名限额按照单位总人数的30%
				if(PreDeptAssignedInfo.ORDINARY_CLASS.equals(validCommond.getClassType())){
					restrictPersonNum=(validCommond.getRestrictPersonNums()[i]+1)/3;
				}
				vCommond.setRestrictPersonNum(restrictPersonNum);//限报人数
				SignupDept department=new SignupDept();
				department.setDepid(validCommond.getIds()[i]);
				vCommond.setDepartment(department);//部门
				vCommond.setClassType(validCommond.getClassType());
				preDeptAssignedInfoService.save(vCommond);
			}
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("添加成功!");
		}else{
			resultState.setMessage("未选中任何单位!");
		}
		return "";
	}
	/**
	 * 删除预分配参学名额
	 * @param querycommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/deletePreDeptAssignedInfo")
	public String deletePreDeptAssignedInfo(
			@ModelAttribute("resultList") PreDeptAssignedInfoQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//获得传过来的参数
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchIds())){
			//调用service层删除预分配参学名额
			boolean result=preDeptAssignedInfoService.deletePreDeptAssignedInfo(queryCommond);
			if (!result) {
				resultState.setMessage("删除失败！");
			}else {
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
				resultState.setMessage("删除成功！");
			}
		}else{
			resultState.setMessage("未选中要删除的单位!");
		}
		return "";
	}
	/**
	 * 确认报名限额
	 * @param querycommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2015-1-25
	 */
	@RequestMapping(value = "/addDeptAssignedInfo")
	public String addDeptAssignedInfo(
			@ModelAttribute("resultList") PreDeptAssignedInfoQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//获得传过来的参数
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchIds())){
			boolean result=false;
			//遍历
			for(String id:queryCommond.getSearchIds()){
				//根据id查询预分配参学名额
				PreDeptAssignedInfoValidCommond pcv=preDeptAssignedInfoService.find(id);
				//将预分配参学名额的值赋值给分配参学名额
				DeptAssignedInfoValidCommond validCommond = new DeptAssignedInfoValidCommond();
				validCommond.setClassType(pcv.getClassType());
				validCommond.setDepartment(pcv.getDepartment());
				validCommond.setRestrictPersonNum(pcv.getRestrictPersonNum());
				validCommond.setYear(pcv.getYear());
				//添加单位年度参学名额
				try{
					deptAssignedInfoService.save(validCommond);
					result=true;
				}catch(Exception e){
					resultState.setMessage("确认报名限额失败！");
					return "";
				}
			}
			//调用service层删除预分配参学名额
			if(result){
				result=preDeptAssignedInfoService.deletePreDeptAssignedInfo(queryCommond);
			}
			if (!result) {
				resultState.setMessage("删除预分配参学名额失败！");
			}else {
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
				resultState.setMessage("确认报名限额成功！");
			}
		}else{
			resultState.setMessage("未选中任何数据!");
		}
		return "";
	}
	
	
}
